package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobaibai.entity.Coupon;
import com.xiaobaibai.entity.CouponBelong;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.ICouponBelongService;
import com.xiaobaibai.service.ICouponService;
import com.xiaobaibai.units.SecurityUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "优惠券接口")
@RequestMapping("/coupon")
@CrossOrigin
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ICouponBelongService couponBelongService;

    @GetMapping("/list")
    @ApiOperation("获取当前可领取的优惠券")
    public ResponseFormat getCouponList(){
//        SecurityContext context = SecurityContextHolder.getContext();
//        Integer userId = (Integer) context.getAuthentication().getPrincipal();
        Integer userId = SecurityUnit.getUserId();
        //获取已经领取的优惠券(所有的)
        Set<Integer> set=couponBelongService.getUserHaveCoupon(userId);
        //当前有效的优惠券
        List<Coupon> couponList = couponService.getCouponList();
        List<Coupon> result=new ArrayList<>();
        couponList.forEach(l->{
            if(!set.contains(l.getId()))
                result.add(l);
        });
        return ResponseFormat.ok("获取可用优惠券信息成功",result);
    }

    @GetMapping("/already")
    @ApiOperation("获取已经领取且可用的优惠券")
    public ResponseFormat getAlreadyCouponList(){
        SecurityContext context = SecurityContextHolder.getContext();
        Integer userId = (Integer) context.getAuthentication().getPrincipal();
        List<Coupon> list = couponBelongService.getOkCouponList(userId);
        return ResponseFormat.ok("获取当前可用优惠券成功",list);
    }

    @GetMapping("/failure")
    @ApiOperation("获取已经领取后失效或者已经使用的优惠券")
    public ResponseFormat getFailureCouponList(){
        SecurityContext context = SecurityContextHolder.getContext();
        Integer userId = (Integer) context.getAuthentication().getPrincipal();
        List<Coupon> list = couponBelongService.getFailureCouponList(userId);
        return ResponseFormat.ok("获取当前不可用优惠券成功",list);
    }

    @GetMapping("/receive/{id}")
    @ApiOperation("领取优惠券")
    public ResponseFormat receiveCoupon(@PathVariable(value = "id") Integer couponId){
        SecurityContext context = SecurityContextHolder.getContext();
        Integer userId = (Integer) context.getAuthentication().getPrincipal();
        boolean flag = couponService.receiveCoupon(userId, couponId);
        if(!flag)
            return ResponseFormat.failure("优惠券不足或者已领取");
        return ResponseFormat.ok("领取成功");
    }

}
